package com.bcbim.api_web.sys.controller;

import com.bcbim.api_web.sys.redis.MyRedisService;
import com.bcbim.authentication_interface.user.dto.LoginUser;
import com.bcbim.authentication_interface.user.dto.UserDTO;
import com.bcbim.authentication_interface.user.po.User;
import com.bcbim.authentication_interface.user.service.UserService;
import com.bcbim.authentication_interface.util.AuthUtil;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.util.AESUtil;
import com.bcbim.common.util.GuidUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 登录
 * @Author: ye
 * @Date: 2021/6/21
 * @Version: V1.0
 **/
@Slf4j
@RestController
public class LoginController {

    @Reference(protocol = "dubbo", group = "authentication", version = "1.0.0")
    private UserService userServiceImpl;

    @Autowired
    private MyRedisService myRedisService;

    /**
     * @Description: 用户登录
     * @Param: username 用户名
     * @Param: password 密码
     * @param: imgCode 验证码
     * @Return: ReturnMsg
     * @Author: haoshuai
     * @Date: 2020/12/11
     */
    @RequestMapping(value = "/userLogin", method = RequestMethod.POST)
    public ReturnMsg userLogin(@RequestParam("username") String username, @RequestParam("password") String password) {

        // 用户名、密码不能为空
        if (StringUtils.isAnyBlank(username, password)) {
            return ReturnMsg.consFail("用户名或密码不能为空！");
        }

        // 根据用户名查找用户信息
        UserDTO userDTO = userServiceImpl.getUserByUsername(username);
        if(ObjectUtils.isEmpty(userDTO) || !password.equals(AESUtil.decrypt(userDTO.getPassword()))) {
            return ReturnMsg.consFail("用户名或密码错误！");
        }

        //更新登录时间
        userServiceImpl.updateLastLoginTime(username);

        // 生成token，写入redis，返回客户端
        LoginUser loginUser = new LoginUser();
        BeanUtils.copyProperties(userDTO, loginUser);

        // 生成token, 并采用AES加密
        String token = AESUtil.encrypt(GuidUtil.get());
        loginUser.setId(AESUtil.encrypt(loginUser.getId())).setToken(token);

        // 生成业务业务 token (pointToken lineToken), 为管网信息管理页面做数据缓存准备
        // 生成方式为 : 用户名 + point/line 字符串 + 时间戳字符串, 然后进行 AES 加密
        String time = String.valueOf(System.currentTimeMillis());
        String pointToken = username + "_" + "point" + "_" + time;
        String lineToken = username + "_" + "line" + "_" + time;
        String healthToken = username + "_" + "health" + "_" + time;
        String pipeLineDownload = username + "_" + "pipeLineDownload" + "_" + time;
        String pipePointDownload = username + "_" + "pipePointDownload" + "_" + time;
        loginUser.setPointToken(pointToken);
        loginUser.setLineToken(lineToken);
        loginUser.setHealthToken(healthToken);
        loginUser.setPipeLineDownloadToken(pipeLineDownload);
        loginUser.setPipePointDownloadToken(pipePointDownload);

        boolean b = myRedisService.set(token, loginUser, Constant.PERMISSION_EXPIRATION_TIME);
        if (b) {
            return ReturnMsg.consSuccess(loginUser, "登录成功！");
        } else {
            log.error("redis server exception...");
            return ReturnMsg.consFail("系统维护，请稍后重试！");
        }
    }

    /**
     * @Description: 用户退出
     * @Return: ReturnMsg
     * @Author: haoshuai
     * @Date: 2020/12/11
     */
    @RequestMapping(value = "/userLogout", method = RequestMethod.POST)
    public ReturnMsg userLogout() {

        // 获取用户token
        String token = AuthUtil.getTokenEncrypt();

        return ReturnMsg.consSuccess();
    }


}
